function [] = computeCorrelation_TheoryFree( dirName ,fileNum ,ncM)
%COMPUTECORRELATION_THEORYFREE Summary of this function goes here
%   Detailed explanation goes here
%---------------------------Preparation-------------------------------%
% this part repeats the "Preparation" of plotEntropyFix

[position,velocity,num]=readText(dirName, fileNum);

S = sum(velocity) / num; % now S is the average velocity vector 
SL = norm(S); % SL equals the model of S
unitVec = S/SL;
sL = velocity * unitVec'; % column vector
pai = velocity - sL * unitVec ;  % get the longitudinal velocity "sL*unitVec" and the perpendicular velocity "pai" 

distance = zeros( num,num );
expCorr = zeros(num,num);
for i= 1 : num
    for j = 1: num
        distance(i,j) = norm(position(i,:)-position(j,:));
        expCorr(i,j) = sum(velocity(i,:).*velocity(j,:));
    end
end % acquire distance and correlation between i and j

%-----------------------Compute Correlation--------------------%

maxR  = floor(max(max(distance)))+1;
deltaR = maxR / 20;
maxN = floor (maxR/deltaR);


ncM=20;
%ncM = max(vectorNc); % find ncMax and compute again
[logic, intCorrelation,~] = computeIntCorrelation( num ,ncM ,distance ,expCorr,true(1,num) );
matrixNij = 0.5 * (logic + logic'); % when i = j ,logic(i,j) = False
diagnoalA_ij = sum(matrixNij);
A_ = diag(diagnoalA_ij) - matrixNij;
[eigVecA_,lamdaMatrix] = eig(A_);
lamda = diag(lamdaMatrix); % redefine
J = 2/(ncM*(1-intCorrelation));
% disp(J)

CorrP_TheoR =zeros(1,maxN);
CorrL_TheoR =zeros(1,maxN);
CorrP_Theory = zeros(num,num);
CorrL_Theory = zeros(num,num);
count_corr = zeros(1,maxN);
for i = 1 : num
    for j = 1:num
        valueP = eigVecA_(i,:).*eigVecA_(j,:)./lamda';
        valueL = (eigVecA_(i,:).^2+eigVecA_(j,:).^2)./lamda';
        valueP(:,1) = [];
        valueL(:,1) = []; % delete the zero eigenvalue
        CorrP_Theory(i,j) = sum(valueP)*2/J;
        CorrL_Theory(i,j) = 1-sum(valueL)/J;
        temp = floor((distance(i,j)/deltaR))+1;
        if temp < maxN
        CorrP_TheoR(temp) = CorrP_TheoR(temp) + CorrP_Theory(i,j);
        CorrL_TheoR(temp) = CorrL_TheoR(temp) + CorrL_Theory(i,j);
        count_corr(floor((distance(i,j)/deltaR))+1) = count_corr(floor((distance(i,j)/deltaR))+1)+1;
        end
    end
end

meanCorrP_TheoR = CorrP_TheoR./count_corr;
meanCorrL_TheoR = CorrL_TheoR./count_corr;
    
%-----------------------------Plot-----------------------------------%
figure(1)
plot(deltaR*(1:maxN),meanCorrL_TheoR,'g')
% title('perpendicular correlation from theory')
figure(2)
plot(deltaR*(1:maxN),meanCorrP_TheoR,'g')
% title('longitudinal correlation from theory (include average velocity)')
figure(3)
plot(deltaR*(1:maxN),meanCorrL_TheoR + meanCorrP_TheoR,'g')
% title('total correlation from theory')

end

